package com.es.web.user.controller;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.es.common.cache.CacheStorage;
import com.es.common.cache.GlobalCacheName;
import com.es.common.cache.RedisTool;
import com.es.common.utils.SpringUtils;
import com.es.web.base.code.Code;
import com.es.web.base.controller.BaseController;
import com.es.web.user.bean.UserInfo;

//@CrossOrigin(origins="http://m.womai.com",methods={RequestMethod.GET},maxAge=3600)//处理这个域的跨域请求
@RestController
@RequestMapping("/user")
public class UserController extends BaseController{
	
	@RequestMapping("/login")
	public String login(@RequestParam(value="mobile",required=true) String mobile ,@RequestParam(value="pwd",required=true) String pwd){
		HttpSession session = getRequest().getSession();
		String sessionid = session.getId();
		CacheStorage cacheStorage = SpringUtils.getCacheFactory().get(GlobalCacheName.PRODUCT_SHARD.getName());
		String key = mobile;
		UserInfo info = RedisTool.getInstance().get(cacheStorage, key, UserInfo.class);
		if (info != null && info.getPwd().equals(pwd)) {
			session.setAttribute(sessionid, info);
			return render(Code.SUCCESS);
		}else {
			return render(Code.FAIL);
		}
	}
	@RequestMapping("/register")
	public String register(@RequestParam(value="name",required=true) String name, @RequestParam(value="mobile",required=true) String mobile ,@RequestParam(value="pwd",required=true) String pwd){
		HttpSession session = getRequest().getSession();
		String sessionid = session.getId();
		CacheStorage cacheStorage = SpringUtils.getCacheFactory().get(GlobalCacheName.PRODUCT_SHARD.getName());
		String key = mobile;
		UserInfo info = RedisTool.getInstance().get(cacheStorage, key, UserInfo.class);
		if (info != null) {
			return render(Code.FAIL);
		}
		info = new UserInfo();
		info.setName(name);
		info.setMobile(mobile);
		info.setPwd(pwd);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String createDate = sdf.format(new Date());
		info.setCreateDate(createDate);
		session.setAttribute(sessionid, info);
		RedisTool.getInstance().set(cacheStorage, key, info);
		return render(Code.SUCCESS);
	}
	
	@RequestMapping("/info")
	public String info(){
		try {
			if (isLogin()) {
				HttpSession session = getSession();
				String sessionid = session.getId();
				int version = getVersion();
				System.out.println(version);
				Object obj = session.getAttribute(sessionid);
				if (obj != null && obj instanceof UserInfo) {
					UserInfo info = (UserInfo) obj;
					return render(Code.SUCCESS,info);
				}else {
					return render(Code.FAIL);
				}
			}else {
				return render(Code.NO_LOGIN);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return render(Code.FAIL);
		}
	}

}
